package com.showcai.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 从spring容器中获取bean
 * @author  徐军
 * @version 创建时间 2012-9-12 下午02:10:41
 */
public class ApplicationContextHolder
  implements ApplicationContextAware
{
  private static Log _$2 = LogFactory.getLog(ApplicationContextHolder.class);
  private static ApplicationContext _$1;

  public void setApplicationContext(ApplicationContext paramApplicationContext)
    throws BeansException
  {
    if (_$1 != null)
      throw new BeanCreationException("ApplicationContextHolder already holded 'applicationContext'.");
    _$1 = paramApplicationContext;
    _$2.info("holded applicationContext,displayName:" + _$1.getDisplayName());
  }

  public static ApplicationContext getApplicationContext()
  {
    if (_$1 == null)
      throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init.");
    return _$1;
  }

  public static Object getBean(String paramString)
  {
    return getApplicationContext().getBean(paramString);
  }
}
